<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermission">添加权限</el-button>
      <el-table :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button size="mini" type="text" @click="addSecondPermission(row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="lookPermission(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 放置一个弹层 用来编辑新增节点 -->
    <el-dialog :title="permission?'编辑权限点':'新增权限点'" :visible.sync="showDialog">
      <!-- 表单 -->
      <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="isOk">确定</el-button>
          <el-button size="small" @click="close">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, delPermission, addPermission, LookPermission, editPermission } from '@/api/permission'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      },
      showDialog: false,
      permission: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async lookPermission(id) {
      this.permission = id
      const res = await LookPermission(id)
      this.formData = res
      this.showDialog = true
    },
    async addSecondPermission(id) {
      this.addPermission()
      this.formData.pid = id
      this.formData.type = this.formData.pid
    },
    close() {
      this.showDialog = false
    },
    async isOk() {
      this.$refs.perForm.validate()
      if (this.permission) {
        //
        await editPermission(this.permission, this.formData)
        this.$message.success('编辑权限成功')
      } else {
        await addPermission(this.formData)
        this.$message.success('新增权限成功')
      }
      this.showDialog = false
      this.getList()
    },
    addPermission() {
      this.permission = ''
      this.$refs.perForm?.resetFields()
      this.formData.description = ''
      this.showDialog = true
      // 添加一级权限，pid应该设置为0
      this.formData.pid = 0
      // 添加一级权限，type应该设置为1
      this.formData.type = 1
    },
    async del(id) {
      try {
        await this.$confirm('是否要删除本权限')
        await delPermission(id)
        this.$message.success('删除成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    async getList() {
      const res = await getPermissionList()
      // console.log(res)
      // 声明一个找儿子的函数
      function getChildren(id) {
        return res.filter((v) => v.pid === id)
      }
      //
      res.forEach((v) => {
        if (getChildren(v.id).length) {
          v.children = getChildren(v.id)
        }
      })

      // 将一级节点找出来
      const list = res.filter((v) => v.pid === 0)

      this.list = list
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
